package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JButton;
import MessagePacks.MessagePack;
import MessagePacks.GetAllPack;
import MessagePacks.UpdatePack;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Random;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

class DetermineSupervisor extends IS_dep_manager{
	
	private static final long serialVersionUID = 1L;
	private MainGUI mainGui;
	private JButton btnApply;
	private GetAllPack detSup; 
	private GetAllPack detSupLvl;
	private JComboBox<String> procBOX;
	private JComboBox<String> supervisorBox;
	private UpdatePack UpdStat;
	private UpdatePack UpdSup;
	private UpdatePack UpdEst;
	private GetAllPack numOfWorkers;

	public DetermineSupervisor(MainGUI mainGui){
		
		
		super(mainGui);
		this.mainGui=mainGui;

		getSupervisorSelect();
		getProccessSelect();
		
		
		//ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		//ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 884, 40);
		getContentPane().add(panel);
		
		JLabel lblDetermineSupervisor = new JLabel("Determine New Supervisor");
		lblDetermineSupervisor.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblDetermineSupervisor);
		
		JLabel lblSelectSupervisor = new JLabel("Select Supervisor");
		lblSelectSupervisor.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSelectSupervisor.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblSelectSupervisor.setBounds(247, 240, 113, 23);
		getContentPane().add(lblSelectSupervisor);
		
		
		
		supervisorBox = new JComboBox<String>();
		supervisorBox.setBounds(370, 243, 139, 20);
		supervisorBox.addItem("");
		getContentPane().add(supervisorBox);
		fillsupBox();
	

		
		
		
		JLabel lblSelectProcess = new JLabel("Select Process:");
		lblSelectProcess.setHorizontalAlignment(SwingConstants.LEFT);
		lblSelectProcess.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblSelectProcess.setBounds(247, 157, 113, 23);
		getContentPane().add(lblSelectProcess);
		
		
		setBtnApply();
		procBOX = new JComboBox<String>();
		fillProcBox();

		

		
		
	}
	
	public JButton getBtnApply() {
		return btnApply;
	}


	public void setBtnApply() {
		btnApply = new JButton("Apply");
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStatus();
				updateSupervisor();
				chooseRandomEstimator();
				JOptionPane.showMessageDialog(serverGUI.mainframe,"new supervisor choosen");
				getProccessSelect();
				procBOX.removeAllItems();
				fillProcBox();
			}
		});
		btnApply.setBounds(398, 289, 89, 23);
		getContentPane().add(btnApply);
	}

	public void getProccessSelect(){
		detSup = new GetAllPack (MessagePack.Detsupervisor);
		mainGui.client.handleMessageFromClientUI(detSup);
		detSup= (GetAllPack) mainGui.client.getMessage();
		
	}
	
	public void getSupervisorSelect(){
		detSupLvl = new GetAllPack (MessagePack.getAllWorkers);
		mainGui.client.handleMessageFromClientUI(detSupLvl);
		detSupLvl= (GetAllPack) mainGui.client.getMessage();
		
	}
	
	public void fillsupBox(){
		Iterator<String> iter2 = detSupLvl.nArray.iterator();
		while (iter2.hasNext()) {
			String supId = iter2.next();
				supervisorBox.addItem(supId);

		}
	}
	
	public void fillProcBox(){
		
		procBOX.setBounds(370, 160, 139, 20);
		getContentPane().add(procBOX);
		procBOX.addItem("");
		Iterator<String> iter = detSup.nArray.iterator();
		while (iter.hasNext()) {
			String procId = iter.next();
				procBOX.addItem(procId);

		}
		if(procBOX.getItemCount()==1){
			btnApply.setEnabled(false);
		}
	}
	
	public void updateStatus(){
		UpdStat = new UpdatePack(MessagePack.updateStatus);
		UpdStat.setStatus("waiting for approval");
		UpdStat.setProcId(Integer.parseInt((String) procBOX.getSelectedItem()));
		mainGui.client.handleMessageFromClientUI(UpdStat);
	}
	
	public void updateSupervisor(){
		UpdSup = new UpdatePack(MessagePack.updateSupervisor);
		UpdSup.setProcId(Integer.parseInt((String) procBOX.getSelectedItem()));
		UpdSup.setWorkerId(Integer.parseInt((String)supervisorBox.getSelectedItem()));
		mainGui.client.handleMessageFromClientUI(UpdSup);
	}
	
	public void chooseRandomEstimator(){
				
			int numOfWrks;
			GetWorkers();
			numOfWrks= numOfWorkers.getArray().size();
			Random rand = new Random();
			int n = rand.nextInt(numOfWrks) + 1;
			n= Integer.parseInt(numOfWorkers.getArray().get(n));
			updateEstimator(n);
		
	}
	public void GetWorkers(){
		numOfWorkers = new GetAllPack(MessagePack.getAllWorkers);
		mainGui.client.handleMessageFromClientUI(numOfWorkers);
		numOfWorkers = (GetAllPack) mainGui.client.getMessage();
	}
	
	/**
	 * 
	 * @param workerID - Worker's ID
	 */
	public void updateEstimator(int workerID){
		UpdEst = new UpdatePack(MessagePack.updateEstimator);
		UpdEst.setProcId(Integer.parseInt((String) procBOX.getSelectedItem()));
		UpdEst.setWorkerId(workerID);
		mainGui.client.handleMessageFromClientUI(UpdEst);
	}
}
